<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="/views/common/taglibs.jsp" %>
<!DOCTYPE>
<html>
<head>
    <!-- 引入通用js、css文件(xiaogq) -->
    <meta name="decorator" content="default"/>
    <!-- 引用ztree css样式文件 -->
    <link type="text/css" rel="stylesheet" href="${ctx}/css/widget/ztree/zTreeStyle.css"/>
    <link rel="stylesheet" type="text/css" href="${ctx}/css/common/operation/sys_operation.css"/>
    <!-- 引用ztree js文件 -->
    <script type="text/javascript" src="${ctx}/js/widget/ztree/jquery.ztree.core.js"></script>
    <script type="text/javascript" src="${ctx}/js/widget/ztree/jquery.ztree.excheck.js"></script>
</head>

<body>
    <!-- 加载列表操作 -->
    <div id="buttonArray" class="button_list">
        <c:if test="${!isAdmin}">
            <a href="javascript:;" onclick="save()">
                <span class="span_button">
                    <span><img src="${ctx}/image/common/operation/oper_SAVE.png"></span>
                    <span name="buTitle">保存</span>
                </span>
            </a>
        </c:if>
        <a href="javascript:;" onclick="javascript:parent.closeTab();">
            <span class="span_button">
                <span><img src="${ctx}/image/common/operation/oper_CANCEL.png"></span>
                <span name="buTitle" title="">取消</span>
            </span>
        </a>
        <a href="javascript:;" onclick="expandNode('1')" >
            <span class="span_button">
                <span><img src="${ctx}/image/common/operation/oper_unfold.png"></span>
                <span name="buTitle" title="">全部展开</span>
            </span>
        </a>
        <a href="javascript:;" onclick="expandNode('0')" >
            <span class="span_button">
                <span><img src="${ctx}/image/common/operation/oper_fold.png"></span>
                <span name="buTitle" title="">全部折叠</span>
            </span>
        </a>
    </div>

    <div class="content-wrap">
        <ul id="menuTree" class="ztree"></ul>
    </div>

    <script type="text/javascript">
        var ztree;
         var setting = {
             view: {
                selectedMulti: false
             },
             check: {
                enable: true
             },
             data: {
                 simpleData: {
                     enable:true,
                     idKey:"id",
                     pIdKey:"pid",
                     rootPId: 0
                 }
             },
             callback: {
                 beforeCheck: beforeCheck,
                 onCheck: onCheck
             }
         };

         $(function(){
             var zTree = $.fn.zTree.init($("#menuTree"), setting, ${menus});
             zTree.expandAll(true);
         });

         var className = "dark";
         function beforeCheck(treeId, treeNode) {
             className = (className === "dark" ? "":"dark");
             //alert("[ "+getTime()+"]  beforeCheck  " + treeNode.name + " * " + treeNode.id );
             return (treeNode.doCheck !== false);
         }

         function onCheck(e, treeId, treeNode) {
             //alert("[ "+getTime()+"]  onCheck  " + treeNode.name + " * " + treeNode.id);
         }

         //保存
         function save(){
             ztree = $.fn.zTree.getZTreeObj("menuTree");
             var nodes = ztree.getCheckedNodes(true);
             var ids = new Array();
             for(var i=0; i<nodes.length; i++){
                ids.push( nodes[i].id );
             }
             $.ajax({
                 type: "POST",
                 contentType: "application/json",
                 url: "${ctx}/"+Constants.updateRolePmsUrl+"?menuids="+ids+"&roleid=${roleid}&kind=${kind}",
                 dataType: "json",
                 success: function(data) {
                     var flag = data.flag;
                     if(!flag){
                         alert("操作权限保存失败!");
                     }
                     parent.closeTab();
                 },
                 error: function() {
                    alert("操作失败!");
                 }
             });
         }

         //全部展开或折叠
         function expandNode(temp){
             zTree = $.fn.zTree.getZTreeObj("menuTree");
             if(temp==1){
                zTree.expandAll(true);
             }else{
                zTree.expandAll(false);
             }
         };
    </script>
</body>
</html>
